"""
统计同一字符串出现的次数
由用户控制字符串长度，随机生成有重复内容的字符串，使用字符串的count方法进行统计
"""
import random
import string

result = {}
# 用户输入需要的字符串长度
userNumber = int(input("请输入需要的字符串长度"))
# 进行长度的分配
mother_len = userNumber // 3
child_len = userNumber - mother_len
print("母串长度:", mother_len)
print("子串长度:", child_len)

n1 = random.randint(1, len(string.digits))
n2 = random.randint(1, len(string.ascii_letters))
n3 = random.randint(1, len(string.punctuation))
print(n1, n2, n3)

# 获取随机长度的指定字符
con1 = random.sample(string.digits, n1)
con2 = random.sample(string.ascii_letters, n2)
con3 = random.sample(string.punctuation, n3)
print(con1, con2, con3)

# 进行相关内容的合并
r1 = ''.join(con1)
r2 = ''.join(con2)
r3 = ''.join(con3)
res = r1 + r2 + r3
print(res)

ori_mother_len = len(res)
print("原始母串长度", ori_mother_len)

# 如果原始母串长度大于规定长度，则进行随机数截取
if ori_mother_len > mother_len:
    r = random.sample(res, mother_len)
    strCon = ''.join(r)
    print("最终字符长度及内容", len(strCon), strCon)
elif ori_mother_len < mother_len:
    tmplist = []
    for i in range(mother_len - ori_mother_len):
        tmplist.append(random.choice(res))
    print("补充字符串长度：", len(tmplist))
    strCon = res + ''.join(tmplist)
    print("最终字符串长度：", len(strCon))
####################################
# 添加重复字符
listr = list(strCon)
number = len(listr)
for i in range(child_len):
    n = random.randint(1, number)
    m = random.randint(0, number)
    tmp = listr[m]
    listr.insert(n, tmp)
strCon = ''.join(listr)
print("最终长度：", len(strCon))
#######################################
# 获取字符串中的每个字符
for i in strCon:
    result[i] = strCon.count(i)

print(result)
